package com.test.reggie.model.address;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.test.reggie.R;
import com.test.reggie.base.BaseActivity;
import com.test.reggie.bean.AddAddressBean;
import com.test.reggie.bean.AddressBookDetailsBean;
import com.test.reggie.bean.State;
import com.test.reggie.bean.StringBean;
import com.test.reggie.bean.UserBean;
import com.test.reggie.databinding.ActivityAddAddressBinding;
import com.test.reggie.http.API;
import com.test.reggie.utils.VerifyUtils;
import com.yechaoa.yutils.GsonUtil;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加收货地址
 */
public class AddAddressActivity extends BaseActivity<ActivityAddAddressBinding, AddAddressPrensenter>
        implements AddAddressView, View.OnClickListener {

    private UserBean userBean;
    private int sex = 1;
    private int tag = 0;
    private String id;

    @Override
    protected AddAddressPrensenter createPresenter() {
        return new AddAddressPrensenter(this);
    }

    @Override
    protected void initView() {
        viewBinding.ic.tvTitle.setText("添加收货地址");
        viewBinding.ic.llBack.setOnClickListener(this::onClick);
        viewBinding.btnSave.setOnClickListener(this::onClick);

        userBean = GsonUtil.GsonToBean(String.valueOf(API.kv.decodeString(State.user)),
                UserBean.class);

        id = getIntent().getStringExtra("id");
        if (id != null) {
            viewBinding.ic.tvTitle.setText("修改收货地址");
            YUtils.showLoading("加载中...");
            presenter.addressBookDetails(id);
        }
    }

    @Override
    protected void initData() {
        viewBinding.rgSex.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_nan:
                    sex = 1;
                    viewBinding.rbNan.setChecked(true);
                    break;
                case R.id.rb_nv:
                    sex = 2;
                    viewBinding.rbNv.setChecked(true);
                    break;
                default:
                    break;
            }
        });

        viewBinding.rgTag.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_wu:
                    tag = 0;
                    viewBinding.rbWu.setChecked(true);
                    viewBinding.rbWu.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                    viewBinding.rbGongsi.setBackgroundColor(0);
                    viewBinding.rbJia.setBackgroundColor(0);
                    viewBinding.rbXuexiao.setBackgroundColor(0);
                    break;
                case R.id.rb_gongsi:
                    tag = 1;
                    viewBinding.rbGongsi.setChecked(true);
                    viewBinding.rbWu.setBackgroundColor(0);
                    viewBinding.rbGongsi.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                    viewBinding.rbJia.setBackgroundColor(0);
                    viewBinding.rbXuexiao.setBackgroundColor(0);
                    break;
                case R.id.rb_jia:
                    tag = 2;
                    viewBinding.rbJia.setChecked(true);
                    viewBinding.rbWu.setBackgroundColor(0);
                    viewBinding.rbGongsi.setBackgroundColor(0);
                    viewBinding.rbJia.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                    viewBinding.rbXuexiao.setBackgroundColor(0);
                    break;
                case R.id.rb_xuexiao:
                    tag = 3;
                    viewBinding.rbXuexiao.setChecked(true);
                    viewBinding.rbWu.setBackgroundColor(0);
                    viewBinding.rbGongsi.setBackgroundColor(0);
                    viewBinding.rbJia.setBackgroundColor(0);
                    viewBinding.rbXuexiao.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void addAddressSuccess(AddAddressBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    @Override
    public void addressBookDetailsSuccess(AddressBookDetailsBean bean) {
        YUtils.hideLoading();
        if (bean.getCode() == 200) {
            viewBinding.etName.setText(bean.getData().getConsignee());

            sex = bean.getData().getSex();
            if (sex == 1) {
                viewBinding.rbNan.setChecked(true);
            } else if (sex == 2) {
                viewBinding.rbNv.setChecked(true);
            }

            viewBinding.etPhone.setText(bean.getData().getPhone());
            viewBinding.etAddress.setText(bean.getData().getDetail());

            tag = bean.getData().getLabel();
            if (tag == 0) {
                viewBinding.rbWu.setChecked(true);
                viewBinding.rbWu.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                viewBinding.rbGongsi.setBackgroundColor(0);
                viewBinding.rbJia.setBackgroundColor(0);
                viewBinding.rbXuexiao.setBackgroundColor(0);
            } else if (tag == 1) {
                viewBinding.rbGongsi.setChecked(true);
                viewBinding.rbWu.setBackgroundColor(0);
                viewBinding.rbGongsi.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                viewBinding.rbJia.setBackgroundColor(0);
                viewBinding.rbXuexiao.setBackgroundColor(0);
            } else if (tag == 2) {
                viewBinding.rbJia.setChecked(true);
                viewBinding.rbWu.setBackgroundColor(0);
                viewBinding.rbGongsi.setBackgroundColor(0);
                viewBinding.rbJia.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                viewBinding.rbXuexiao.setBackgroundColor(0);
            } else if (tag == 3) {
                viewBinding.rbXuexiao.setChecked(true);
                viewBinding.rbWu.setBackgroundColor(0);
                viewBinding.rbGongsi.setBackgroundColor(0);
                viewBinding.rbJia.setBackgroundColor(0);
                viewBinding.rbXuexiao.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
            }
        } else {
            ToastUtil.showCenter(bean.getMsg());
        }
    }

    @Override
    public void updateAddressBookSuccess(StringBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            Intent intent = new Intent();
            intent.putExtra("name", viewBinding.etName.getText().toString().trim());
            intent.putExtra("sex", sex);
            intent.putExtra("phone", viewBinding.etPhone.getText().toString().trim());
            intent.putExtra("address", viewBinding.etAddress.getText().toString().trim());
            intent.putExtra("tag", tag);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save://新增地址
                if (TextUtils.isEmpty(viewBinding.etName.getText().toString().trim())) {
                    ToastUtil.showCenter("联系人不能为空");
                } else if (TextUtils.isEmpty(viewBinding.etPhone.getText().toString().trim())) {
                    ToastUtil.showCenter("手机号不能为空");
                } else if (!VerifyUtils.phone(viewBinding.etPhone.getText().toString().trim())) {
                    ToastUtil.showCenter("手机号格式不正确");
                } else if (TextUtils.isEmpty(viewBinding.etAddress.getText().toString().trim())) {
                    ToastUtil.showCenter("地址不能为空");
                } else {
                    YUtils.showLoading("提交中...");
                    Map<String, Object> mapAddAddress = new HashMap<>();
                    mapAddAddress.put("userId", userBean.getData().getId());//用户id
                    mapAddAddress.put("consignee", viewBinding.etName.getText().toString().trim());//联系人
                    mapAddAddress.put("sex", sex);//性别
                    mapAddAddress.put("phone", viewBinding.etPhone.getText().toString().trim());//手机号
                    mapAddAddress.put("detail", viewBinding.etAddress.getText().toString().trim());//详细地址
                    mapAddAddress.put("label", tag);//标签
                    if (id != null) {
                        mapAddAddress.put("id", id);//id
                        presenter.updateAddressBook(mapAddAddress);//修改地址
                    } else {
                        presenter.addAddress(mapAddAddress);//新增地址
                    }
                }
                break;
            case R.id.ll_back:
                finish();
                break;
            default:
                break;
        }
    }
}